package atom.core0.util;

import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;

public class MapUtil
{
	static protected final Logger	log	= Logger.getLogger(MapUtil.class);
	public static void log(Map map)
	{
		Iterator it=map.entrySet().iterator();
		while(it.hasNext()){
		    Map.Entry entry=(Map.Entry)it.next();
		    String key = (String)entry.getKey();
		    Object value = entry.getValue();
		    log.debug("{"+key+"="+value+"}");
		}		
		
	}
    //Map的key(数据库字段)会去掉下划线 然后跟beanfield比较,而且 不区分大小写
	//遇到第一个相似的就返回     MAP 重有user_name USERNAME 取值时遇到第一个
	public static Object getMapValueIgnoreCase(Map<String, Object> map,String beanfield)
    {
		Iterator it=map.entrySet().iterator();
		while(it.hasNext()){
		    Map.Entry entry=(Map.Entry)it.next();
		    String key = (String)entry.getKey();
		    String newkey  = key.replaceAll("_", "");
		    Object value = entry.getValue();
		    if (newkey.compareToIgnoreCase(beanfield)==0)
		    	return value;
		}    	
    	return null;
    }
	public static String getMapKeyIgnoreCase(Map<String, Object> map,String beanfield)
    {
		Iterator it=map.entrySet().iterator();
		while(it.hasNext()){
		    Map.Entry entry=(Map.Entry)it.next();
		    String key = (String)entry.getKey();
		    String newkey  = key.replaceAll("_", "");
		    Object value = entry.getValue();
		    if (newkey.compareToIgnoreCase(beanfield)==0)
		    	return key;
		}    	
    	return null;
    }
	
	public static void print(Map map)
	{
		Iterator it=map.entrySet().iterator();
		while(it.hasNext()){
		    Map.Entry entry=(Map.Entry)it.next();
		    String key = (String)entry.getKey();
		    Object value = entry.getValue();
		    System.out.println("{"+key+"="+value+"}");
		}			
	}	
}
